package org.to_zero.springboot.task.service;

import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.Objects;

@Service
public class MailService {

    @Resource
    JavaMailSenderImpl mailSender;

    public void sendMail(){
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        MimeMessageHelper messageHelper = null;
        try {
            messageHelper = new MimeMessageHelper(mimeMessage, true);
            messageHelper.addAttachment("sky.jpg", new File("D:\\imgs\\6459719_p0.jpg"));
            messageHelper.addAttachment("fireworks.jpg",new File("D:\\imgs\\illust_85591387_20201114_134539.jpg"));
            messageHelper.setSubject("让我康康");
            messageHelper.setText("<b style='color:red'>hello world</b>", true);
            messageHelper.setFrom("moonholder@qq.com");
            messageHelper.setTo("yukiralink@gmail.com");
        } catch (MessagingException e) {
            e.printStackTrace();
        }
        mailSender.send(Objects.requireNonNull(messageHelper).getMimeMessage());
    }
}
